package com.example.controller;

import com.example.dto.LoginResult;
import com.example.pojo.TempUser;
import com.example.service.UserService;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Configuration
@Controller
@RequestMapping("/api/user")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }
    


    // 根据用户名和密码登录------------------------------------------------->
    @ResponseBody
    @PostMapping("/login")
    private ResponseEntity<?> login(@RequestParam String username, @RequestParam String password) {
        LoginResult loginResult = userService.loginCheck(username, password);
        if (loginResult.getMessage().equals("用户不存在")) {
            System.out.println("11");
            return ResponseEntity.ok(Map.of("message", "用户不存在", "data",""));
        } else if (loginResult.getMessage().equals("密码错误")) {
            return ResponseEntity.ok(Map.of("message", "密码错误", "data",""));
        }
        return ResponseEntity.ok(Map.of("message", "登录成功", "data", loginResult.getData()));
    }

//    @ResponseBody
    @GetMapping("/loginPage")
    public String showLoginPage(){
        return "join";
    }

    //邮箱登录------------------------------------------------------------>
    @ResponseBody
    @PostMapping("/loginEmail")//发送验证
    public ResponseEntity<?> loginEmail(@RequestParam String email) {
        String result = userService.sendEmailCode(email);

        if ("NOT_FOUND".equals(result)) {
            return ResponseEntity.ok()
                    .contentType(MediaType.valueOf("application/json;charset=UTF-8"))
                    .body(Map.of("code", 200, "msg", "该邮箱未注册"));
        }
        return ResponseEntity.ok(Map.of("code", 200, "msg", "验证码已发送，请查收"));
    }

    @ResponseBody
    @PostMapping("/loginByCode")
    public ResponseEntity<?> loginByCode(@RequestParam String email,
                                         @RequestParam(required = false) String code) {
        if (code == null || code.trim().isEmpty()) {
            userService.sendCode(email); // 不用返回判断了，发就是了
            return ResponseEntity.ok("验证码已重新发送，请查收");
        } else {
            // 有验证码，验证验证码
            boolean success = userService.verifyEmailCode(email, code);
            if (!success) {
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("验证码错误或已过期");
            }
            return ResponseEntity.ok("登录成功");
        }
    }

    //注册账号------------------------------------------------------------>
    //账号密码
    @ResponseBody
    @PostMapping("/registerUserInfo") //@RequestBody User user Spring boot 框架自动将相应Json文件转换成User类
    public ResponseEntity<?> registerUserInfo(@RequestParam String username,
                                              @RequestParam String password,
                                              @RequestParam String email) {
        System.out.println(username+password+email);
        TempUser user = new TempUser();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        // 校验用户名是否已存在
        boolean isUsernameExist = userService.isUsernameExist(user.getUsername());
        if (isUsernameExist) {
            return ResponseEntity.ok("账号已存在，请登录");
        }

        // 用户名不存在，暂存到临时存储
        if (userService.saveUserTemp(user) != 0) {
            return ResponseEntity.ok("账号可用，跳转到邮箱绑定");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("系统异常");
        }
    }

    //邮箱绑定
    @ResponseBody
    @PostMapping("/registerEmail")
    public ResponseEntity<?> registerEmail(@RequestParam String username, @RequestParam String email, @RequestParam String code) {
        //System.out.println(username+email+code);
        if (code == null || code.trim().isEmpty()) {
            if (userService.findByEmail(email) == null) {
                userService.sendCode(email);
                System.out.println(username+email+code);
                return ResponseEntity.ok("验证码已发送，请查收");
            }
            return ResponseEntity.status(HttpStatus.CONFLICT).body("邮箱已被绑定");
        } else {
            // 有验证码，验证验证码
            boolean success = userService.verifyEmailCode(email, code);
            //1
            System.out.println(success);
            //
            if (!success) {
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("验证码错误或已过期");
            }
            if (userService.saveUser(username) != 0) {
                userService.deleteUserTemp(username);
                return ResponseEntity.ok("注册成功");
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("系统异常");
            }
        }
    }



}